package tt.dz.egobus.servicemanager.business.task;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.converter.Converter;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.task.job.DriverGrabBusinessTimeout;
import tt.dz.egobus.servicemanager.business.task.job.QuartzDynamicTask;
import tt.dz.egobus.servicemanager.business.task.job.UserRequestBusinessTimeout;
import tt.dz.egobus.servicemanager.core.task.DynamicTask;
import tt.dz.egobus.servicemanager.core.task.DynamicTaskManager;
import tt.dz.egobus.servicemanager.service.BusinessCacheService;
import tt.dz.egobus.servicemanager.service.DynamicTaskService;

import java.util.Date;

/**
 * @author Anson
 * @date 17-8-10
 * @name 用户请求动态任务添加处理器
 * @since 1.0.6
 */
public class UserRequestDynamicHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserRequestDynamicHandler.class);

    private final String businessId;
    private final BusinessCacheService businessCacheService;
    private final DynamicTaskManager dynamicTaskManager;
    private final DynamicTaskService dynamicTaskService;

    public UserRequestDynamicHandler(String businessId, BusinessCacheService businessCacheService,
                                     DynamicTaskManager dynamicTaskManager, DynamicTaskService dynamicTaskService) {
        this.businessId = businessId;
        this.businessCacheService = businessCacheService;
        this.dynamicTaskManager = dynamicTaskManager;
        this.dynamicTaskService = dynamicTaskService;
    }

    public void handler() {
        LOGGER.info("当前businessId={},添加动态定时任务开始", this.businessId);
        if (StringUtils.isBlank(this.businessId)) {
            return;
        }
        BusinessCache cache = this.businessCacheService.get(this.businessId);
        if (cache == null) {
            LOGGER.info("当前businessId={},未获取到当前缓存业务信息", this.businessId);
            return;
        }
        long curDate = System.currentTimeMillis();
        //用户超时任务处理
        // 秒转毫秒
        Long userRequestTimeout = cache.getLimitTimeoutSeconds() * 1000;
        this.addDynamicTask(cache, curDate, userRequestTimeout, UserRequestBusinessTimeout.class);

        //抢单超时任务处理
        // 秒转毫秒
        Long grabTimeout = cache.getGrabLimitTimeoutSeconds() * 1000;
        this.addDynamicTask(cache, curDate, grabTimeout, DriverGrabBusinessTimeout.class);
    }

    private void addDynamicTask(BusinessCache cache, long curTime, long timeoutMils,
                                Class<? extends QuartzDynamicTask> clazz) {
        Converter<BusinessCache, DynamicTask> taskConverter =
                DynamicTask.jobNameConvert(clazz, new Date(curTime + timeoutMils));
        DynamicTask dynamicTask = taskConverter.convert(cache);
        //添加动态任务缓存
        this.dynamicTaskService.addDynamicTask(dynamicTask);
        this.dynamicTaskManager.addJob(dynamicTask);
    }

    public String getBusinessId() {
        return businessId;
    }

    public BusinessCacheService getBusinessCacheService() {
        return businessCacheService;
    }

    public DynamicTaskManager getDynamicTaskManager() {
        return dynamicTaskManager;
    }

    public DynamicTaskService getDynamicTaskService() {
        return dynamicTaskService;
    }
}
